package com.hellobike.haxing.modules.purchase.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hellobike.haxing.common.annotation.AutoLog;
import com.hellobike.haxing.common.system.base.controller.BaseController;
import com.hellobike.haxing.common.system.query.QueryGenerator;
import com.hellobike.haxing.common.system.vo.LoginUser;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.modules.purchase.entity.TPurchaseProRecord;
import com.hellobike.haxing.modules.purchase.service.ITPurchaseProRecordService;
import com.hellobike.haxing.modules.supplier.entity.TSupplierInfo;
import com.hellobike.haxing.modules.supplier.service.ITSupplierInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.stream.Collectors;
 /**
 * @Description: t_purchase_pro_record
 * @Author: haxing-boot
 * @Date:   2020-03-04
 * @Version: V1.0
 */
@RestController
@RequestMapping("/purchase/tPurchaseProRecord")
@Slf4j
public class TPurchaseProRecordController extends BaseController<TPurchaseProRecord, ITPurchaseProRecordService> {
	@Autowired
	private ITPurchaseProRecordService tPurchaseProRecordService;
	 @Autowired
	 private ITSupplierInfoService tSupplierInfoService;
	/**
	 * 分页列表查询
	 *
	 * @param tPurchaseProRecord
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "t_purchase_pro_record-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(TPurchaseProRecord tPurchaseProRecord,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<TPurchaseProRecord> queryWrapper = QueryGenerator.initQueryWrapper(tPurchaseProRecord, req.getParameterMap());
		Page<TPurchaseProRecord> page = new Page<TPurchaseProRecord>(pageNo, pageSize);
		IPage<TPurchaseProRecord> pageList = tPurchaseProRecordService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	 /**
	  * 列表查询
	  *
	  * @param tPurchaseProRecord
	  * @return
	  */
	 @AutoLog(value = "t_purchase_pro_record-分页列表查询")
	 @GetMapping(value = "/listRecord")
	 public Result<?> listRecord(TPurchaseProRecord tPurchaseProRecord) {
		 tPurchaseProRecord.setSupplierId(getSupplierId());
		 IPage<TPurchaseProRecord> pageList = new Page<>();
		 pageList.setRecords(tPurchaseProRecordService.getRecord(tPurchaseProRecord));
		 return Result.ok(pageList);
	 }

	 private Long getSupplierId(){
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 if(sysUser == null){
			 return null;
		 }
		 QueryWrapper<TSupplierInfo> tSupplierInfoQueryWrapper = new QueryWrapper<>();
		 tSupplierInfoQueryWrapper.eq("mobile", sysUser.getPhone());
		 TSupplierInfo tSupplierInfo = tSupplierInfoService.getOne(tSupplierInfoQueryWrapper);
		 if(tSupplierInfo == null){
			 return null;
		 }
		 return tSupplierInfo.getId();
	 }

	/**
	 *   添加
	 *
	 * @param tPurchaseProRecord
	 * @return
	 */
	@AutoLog(value = "t_purchase_pro_record-添加",operateType=2)
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody TPurchaseProRecord tPurchaseProRecord) {
		tPurchaseProRecordService.save(tPurchaseProRecord);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param tPurchaseProRecord
	 * @return
	 */
	@AutoLog(value = "t_purchase_pro_record-编辑",operateType=3)
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody TPurchaseProRecord tPurchaseProRecord) {
		tPurchaseProRecordService.updateById(tPurchaseProRecord);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "t_purchase_pro_record-删除",operateType=4)
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) Long id) {
		tPurchaseProRecordService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "t_purchase_pro_record-批量删除",operateType=4)
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.tPurchaseProRecordService.removeByIds(Arrays.asList(ids.split(",")).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList()));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) Long id) {
		TPurchaseProRecord tPurchaseProRecord = tPurchaseProRecordService.getById(id);
		if(tPurchaseProRecord==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(tPurchaseProRecord);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param tPurchaseProRecord
    */
    @AutoLog(value = "t_purchase_pro_record-导出excel",operateType=6)
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, TPurchaseProRecord tPurchaseProRecord) {
        return super.exportXls(request, tPurchaseProRecord, TPurchaseProRecord.class, "t_purchase_pro_record");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @AutoLog(value = "t_purchase_pro_record-导入excel",operateType=5)
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, TPurchaseProRecord.class);
    }

}
